package com.apotik.legenda.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.model.SalesTrx;
import com.apotik.legenda.model.SalesTrxObat;
import com.apotik.legenda.util.HibernateUtil;

public class SalesTrxObatDao {

	public boolean saveOrUpdate(SalesTrxObat obj){
		boolean isSucces = false;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.saveOrUpdate(obj);
			session.getTransaction().commit();
			session.close();
			isSucces = true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}

		return isSucces;
	}
	
	public boolean delete(SalesTrxObat obj){
		boolean isSucces = false;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(obj);
			session.getTransaction().commit();
			session.close();
			isSucces = true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}

		return isSucces;
	}
	
	@SuppressWarnings({ "rawtypes" })
	public BigDecimal sumAmountTrxBySalesTrx(SalesTrx salesTrx){
		BigDecimal bg = BigDecimal.ZERO;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(SalesTrxObat.class);
			cr.add(Restrictions.eq("salesTrx", salesTrx));
			cr.setProjection(Projections.sum("amountTrxObat"));
			List list = cr.list();
			
			if(list!=null){
				if(list.size()>0){
					bg = (BigDecimal) list.get(0);
				}
			}
			
			session.close();
		} catch (Exception e) {
			System.out.println("Exception: "+e.getMessage());
		}
		
		return bg;
	}
	
	@SuppressWarnings("unchecked")
	public List<SalesTrxObat> listBySalesTrx(SalesTrx salesTrx){
		List<SalesTrxObat> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria cr = session.createCriteria(SalesTrxObat.class);
			cr.add(Restrictions.eq("salesTrx", salesTrx));
			
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public BigDecimal sum(String propertyName, Criterion...criterions){
		BigDecimal result = BigDecimal.ZERO;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Criteria cr = session.createCriteria(SalesTrxObat.class);
			
			if (criterions!=null) {
				for(Criterion crit : criterions) {
					cr.add(crit);
				}
			}
			cr.setProjection(Projections.sum(propertyName));
			List<BigDecimal> list = cr.list();
			
			if(list.size()>0){
				result = list.get(0);
			}
			
		}catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		} finally {
			session.close();
		}
		
		return result;
	}
	
}
